// pages/invitationbox/partnerlist/partnerlist.js
import {
  getproxyList,
  proxyDel
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    curitem: 1, //1代理商，2供应商
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    let teamId = options.team_id ? options.team_id : "";

    getproxyList(2, teamId, (r) => {
      if (r.code == 0) {
        self.setData({
          teamId: teamId,
          dataInfo: r.data
        })
      } else {
        errorMsg(r.message)
      }
    })
  },

  changeTab: function (e) {
    let self = this;
    let {
      curitem,
      teamId
    } = this.data;
    let curTab = e.currentTarget.dataset.index;
    getproxyList((parseInt(curTab)+1), teamId, (r) => {
      if (r.code == 0) {
        self.setData({
          teamId: teamId,
          curitem: curTab,
          dataInfo: r.data
        })
      } else {
        errorMsg(r.message)
      }
    })
  },
  //切换代理，供应商列表
  changeItem: function() {
    let {
      curitem,
      teamId
    } = this.data;
    let self = this;
    if (curitem == 1) {
      getproxyList(3, teamId, (r) => {
        if (r.code == 0) {
          self.setData({
            teamId: teamId,
            curitem: 2,
            dataInfo: r.data
          })
        } else {
          errorMsg(r.message)
        }
      })

    } else {
      getproxyList(2, teamId, (r) => {
        if (r.code == 0) {
          self.setData({
            teamId: teamId,
            curitem: 1,
            dataInfo: r.data
          })
        } else {
          errorMsg(r.message)
        }
      })

    }
  },
  //邀请供应商
  invGongYing: function() {
    wx.navigateTo({
      url: '../invgongying/invgongying?team_id=' + this.data.teamId,
    })
  },
  //邀请代理商
  invDaiLi: function() {
    wx.navigateTo({
      url: '../invdaili/invdaili?team_id=' + this.data.teamId,
    })
  },
  //删除代理，删除供应商
  delUser: function(e) {
    let id = e.currentTarget.dataset.id;
    let self = this;
    let {
      curitem,
      teamId
    } = this.data;
    wx.showModal({
      title: '删除伙伴',
      content: '确定要删除该合作伙伴吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          proxyDel(id, (parseInt(curitem)+1), (r) => {
            if (r.code == 0) {
              getproxyList((parseInt(curitem)+1), teamId, (r) => {
                if (r.code == 0) {
                  self.setData({ 
                    dataInfo: r.data
                  }) 
                } else {
                  errorMsg(r.message)
                }
              })

            } else {
              errorMsg(r.message)
            }
            errorMsg(r.message)
          })

        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  }
})